package com.wyedu.qqServer.service;

import java.io.IOException;
import java.net.Socket;
import java.util.HashMap;
import java.util.Iterator;

public class ManageServerConnectClientThread {
    private static HashMap<String,ServerConnectClientThread> map = new HashMap<>();

    //添加线程对象到map集合
    public static void addServerConnectClientThread(String id,ServerConnectClientThread serverConnectClientThread) {
        map.put(id,serverConnectClientThread);
    }
    public static ServerConnectClientThread getServerConnectClientThread(String id) {
        return map.getOrDefault(id,null);
    }
    public static void dleServerConnectClientThread(String id){
        //获取该连接 + 删除该连接
        try {
            Socket socket = getServerConnectClientThread(id).getSocket();
            map.remove(id);//此时将该连接释放并删除\
            System.out.println("用户 "+id+" 与服务器断开连接[ "+socket.getInetAddress().getHostAddress()+" / "+socket.getPort()+"]");

            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    public static String getOnLineUsers(){
        //返货在线用户列表

        Iterator<String> iterator = map.keySet().iterator();
        StringBuffer sb = new StringBuffer();
        while (iterator.hasNext()){
            sb.append(iterator.next()).append(" ");
        }
        return sb.toString();
    }

}
